<?php

require 'SmartyMplayer.php';
include_once("ID3.class.php");
include_once("ID3Mapper.class.php");
include_once("playlist.class.php");
include_once("playlistMapper.class.php");
include_once("login.php");

session_set_cookie_params(60*10);
session_name("Music");
session_start();

//session_destroy();
ini_set('display_errors', 'Off');

$smarty = new SmartyMPlayer;

// --------------
// Initialize Part
// --------------

if (!isset($_POST["content"])) $smarty->assign('content', 0);
else $smarty->assign('content', $_POST["content"]);
if (!isset($_SESSION["song"])) $smarty->assign('song', "../project/Musica/prueba.ogg");
else $smarty->assign('song', "../project/Musica/".$_SESSION["song"]);

$smarty->assign('try_upload', false);
$smarty->assign('upload_incorrect', false);

// --------------
// Default Playlist Part
// --------------

if (!isset($_SESSION['default_playlist'])) {
    $default_playlist = new Playlist(1, "default", null, 1); // Es propiedad del administrador
    $default_playlist->createXML();

    $id3 = new ID3(1, "U2", "Elevation", "1", "1850", "U2 Songs", "(1850)1.Elevation - U2[U2 Songs].ogg", 1, true);
    $default_playlist->add_song($id3);
    $id3mpr = new ID3Mapper();
    $id3mpr->insert($id3);
 
    $id3 = new ID3(2, "The Beatles", "Yellow Submarine", "1", "1850", "Yellow Submarine", "(1850)1.Yellow Submarine - The Beatles[Yellow Submarine].ogg", 1, true);
    $default_playlist->add_song($id3);
    $id3mpr = new ID3Mapper();
    $id3mpr->insert($id3);
    
    $id3 = new ID3(3, "Avril Lavigne", "Girlfriend", "1", "2001", "Girlfriend", "(2001)1.Girlfriend - Avril Lavigne[Girlfriend].ogg", 1, true);
    $default_playlist->add_song($id3);
    $id3mpr = new ID3Mapper();
    $id3mpr->insert($id3);

    $plstmpr = new PlaylistMapper();
    $plstmpr->insert($default_playlist);

    $_SESSION['default_playlist'] = $default_playlist->get_playlist();
    foreach($_SESSION['default_playlist'] as $pl_song) {
	$Song = ID3Mapper::findById($pl_song['id']);
	$_SESSION[$pl_song['title']] = $Song->get_file_name();
    }

    $smarty->assign('songs', $_SESSION['default_playlist']);
}
else $smarty->assign('songs', $_SESSION['default_playlist']);

//--------------
// Register Part
//--------------

if (isset($_GET["register"])) {
   $_POST["content"] = 1;
   $smarty->assign('content', 1);
}

//---------------
// Create Playlist Part
//---------------

if (isset($_GET["create_new_playlist"]) && isset($_SESSION["userid"])) {
    $_POST["content"] = 6;
    $smarty->assign('content', 6);
    $smarty->assign('try_create_playlist', true);
    if (isset($_POST["new_playlist"])) {
        try {
            $playlist = new Playlist(null, $_POST["playlist_name"], null, $_SESSION["userid"]);
            $mapper = new PlaylistMapper();
            $id = $mapper->insert($playlist);
            $playlist->set_id($id);
            $playlist->createXML();
            $mapper->update($playlist);
            $smarty->assign('playlist_incorrect', false);
        }
        catch(Exception $e) {
            $smarty->assign('playlist_incorrect', true);
        }
    }
    else $smarty->assign('try_create_playlist', false);
}

//---------------
// Configure playlists
//---------------

if (isset($_GET['configure_playlists']) && isset($_SESSION["userid"])){
    $_POST["content"] = 7;
    $smarty->assign('content', 7);
    $songs = ID3Mapper::findPublicID3AndUserPrivateSongs($_SESSION['userid']);
    if ($songs) {
	foreach($songs as $song) {
	    $playlist[] = $song->convert_to_array();
	}
    }
    else $playlist = null;
    $smarty->assign('configure', true);
    $smarty->assign('songs', $playlist);
}

//---------------
// Show Playlist Part
//---------------

if (isset($_GET["name_playlist"]) && isset($_SESSION["userid"])) {
    $_POST["content"] = 2;
    $smarty->assign('content', 2);
    $plsts = PlaylistMapper::findByUseridAndName($_SESSION["userid"], $_GET["name_playlist"]);
    if ($plsts) {
	$playlist = $plsts[0]->get_playlist();
	if (!$playlist) $playlist = null;
    }
    else $playlist = null;
    $smarty->assign('songs', $playlist);
}

// --------------
// Upload Song Part
// --------------

if (isset($_GET["add_song"]) && isset($_SESSION["userid"])) {
    $_POST["content"] = 3;
    $smarty->assign('content', 3);
}
if (isset($_POST["upload_song"]) && isset($_SESSION["userid"])) {
    $_POST["content"] = 3;
    $smarty->assign('content', 3);
    $smarty->assign('try_upload', true);
    try {
        $music = new ID3(null, ucfirst($_POST["artist"]), ucfirst($_POST["title"]), $_POST["track"],
                 $_POST["year"], ucfirst($_POST["album"]), $_FILES["file"]["name"], $_SESSION["userid"]);
        $smarty->assign('upload_incorrect', false);
        $mapper = new ID3Mapper();
        $id = $mapper->insert($music);
        $music->set_id($id);
	$music->upload_song($_FILES["file"]["tmp_name"], "./Musica/".$music->get_file_name());
    }
    catch (Exception $e) {
        $smarty->assign('upload_incorrect', true);
        $smarty->assign('error_message', $e->getMessage());
    }
}

// --------------
// My Songs Part
// --------------

if (isset($_GET["my_songs"]) && isset($_SESSION["userid"])) {
    $_POST["content"] = 5;
    $smarty->assign('content', 5);
    $id3s = ID3Mapper::findByUserid($_SESSION["userid"]);
    if ($id3s) {
	foreach($id3s as $id3) {
	    $_SESSION[$id3->get_title()] = $id3->get_file_name();
	    $song = $id3->convert_to_array();
	    $songs[] = $song;
            $smarty->assign('upload_incorrect', false);
	}
	$smarty->assign('songs', $songs);
    }
    else $smarty->assign('songs', null);
}

// --------------
// Log In Part
// --------------

if (isset($_POST["submit_login"])) {
    $login = login();
    if($login) {
        $smarty->assign('logged', true);
        $smarty->assign('user', $_SESSION["username"]);
        $smarty->assign('mail', $_SESSION['email']);
    }
    else $smarty->assign('logged', false);
}
else if (!isset($_SESSION["username"]) || (isset($_GET["logout"]) && $_GET["logout"])) {
    $smarty->assign('logged', false);
    unset($_SESSION["userid"]);
    unset($_SESSION["username"]);
    unset($_SESSION["email"]);
}
else {
    $smarty->assign('logged', true);
    $smarty->assign('user', $_SESSION["username"]);
    $smarty->assign('mail', $_SESSION['email']);
}

// -------------------
// User Play List Part
// -------------------

if (isset($_SESSION["userid"])) {
    $plsts = PlaylistMapper::findByUserid($_SESSION["userid"]);
    if ($plsts) {
	foreach($plsts as $playlist)
	    $playlists[] = $playlist->get_name();
	$smarty->assign('playlists', $playlists);
    }
    else $smarty->assign('playlists', null);
}

// -------------------
// User profile part
// -------------------

if (isset($_SESSION['userid']) && isset($_GET['my_profile'])) {
    $_POST['content'] = 4;
    $smarty->assign('content', 4);
}

// --------------
// Display
// --------------

$smarty->display('index.tpl');

?>
